package com.bbzn.device.client.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dao.mqttsub.EmqApiEntity;
import com.bbzn.device.client.dao.mqttsub.EmqApiPublishEntity;
import com.bbzn.device.client.service.EmqApiService;
import com.bbzn.device.client.utils.GetSecret;
import com.bbzn.device.client.utils.HttpUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wulongbo
 * @Date 2021/12/11 9:32
 * @Version 1.0
 */
@Service
public class EmqApiServiceImpl implements EmqApiService {

    /**
     * EMQ 指定客户端订阅/取消订阅 topic
     *
     * @param url
     * @param entity
     */
    @Override
    public void subscribe(String url, EmqApiEntity entity) {
        String authorization = GetSecret.getHeader();
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization", authorization);
        HttpUtils.postMap(url, headerMap, JSONObject.toJSONString(entity));
    }

    /**
     * EMQ 指定客户端批量订阅 topic
     *
     * @param url
     * @param entityList
     */
    @Override
    public void subscribeBatch(String url, List<EmqApiEntity> entityList) {
        String authorization = GetSecret.getHeader();
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization", authorization);
        HttpUtils.postMap(url, headerMap, JSONObject.toJSONString(entityList));
    }

    @Override
    public void publishBatch(String url, List<EmqApiPublishEntity> entityList) {
        String authorization = GetSecret.getHeader();
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization", authorization);
        HttpUtils.postMap(url, headerMap, JSONObject.toJSONString(entityList));
    }

}
